package sgg_hsp_data_structures._01_sparsearray;

/**
 * 稀疏数组
 */
public class SparseArray {

    public static void main(String[] args) {

        int[][] chessArr1 = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;

        int sumNum = 0;

        for (int[] row : chessArr1) {
            for (int data : row) {
                System.out.printf("%d\t", data);
                if (data > 0) {
                    sumNum++;
                }
            }
            System.out.println();
        }
        System.out.println("maxNum = " + sumNum);


        int[][] sparseArr = new int[sumNum + 1][3];

        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sumNum;

        int count = 0;

        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArr1[i][j];
                }
            }
        }


        System.out.println("Result:");
        for (int i = 0; i < sparseArr.length; i++) {
            System.out.printf("%d\t%d\t%d\t\n", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
        }

        int[][] chessArr2 = new int[sparseArr[0][0]][sparseArr[0][1]];


        for (int i = 1; i < sparseArr.length; i++) {
            chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }

        System.out.println("chessArr2:");

        for (int[] row : chessArr2) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }

    }
}